package school.xauat.offershow.serice.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import school.xauat.offershow.mapper.*;
import school.xauat.offershow.pojo.Comment;
import school.xauat.offershow.pojo.Entry;
import school.xauat.offershow.pojo.dto.EntryResponseType;
import school.xauat.offershow.serice.EntryService;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/12/16 0:40
 * @description：
 */
@Service
public class EntryServiceImpl implements EntryService {

    @Autowired
    EntryMapper entryMapper;

    @Autowired
    BusinessMapper businessMapper;

    @Autowired
    EducationMapper educationMapper;

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    StarsMapper starsMapper;

    @Override
    public List<EntryResponseType> list(Integer pageId, String order) {
        Page<Entry> page = new Page<>(pageId, 10);
        QueryWrapper<Entry> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc(order);
        entryMapper.selectPage(page, wrapper);
        List<Entry> list = page.getRecords();
        return getEntryResponseTypes(list);
    }

    @Override
    public boolean add(Entry entry) {
        int insert = entryMapper.insertEntry(entry);
        if (insert > 0) {
            return true;
        } else {
            throw new RuntimeException("插入失败");
        }
    }

    @Override
    public EntryResponseType getById(Integer user_id, Integer entry_id) {
        Entry entry = entryMapper.selectById(entry_id);
        entry.setViews(entry.getViews() + 1);
        entryMapper.updateById(entry);
        EntryResponseType entryResponseType = new EntryResponseType();
        entryResponseType.setId(entry.getId());
        entryResponseType.setBusiness(businessMapper.selectById(entry.getBusinessId()).getName());
        entryResponseType.setCity(entry.getCity());
        entryResponseType.setCompany(entry.getCompany());
        entryResponseType.setPosition(entry.getPosition());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        entryResponseType.setPublish_time(format.format(entry.getPublishTime()));
        entryResponseType.setSalary(entry.getSalary());
        entryResponseType.setViews(entry.getViews());
        entryResponseType.setUser_id(entry.getUserId());
        entryResponseType.setEducation(educationMapper.selectById(entry.getEducationId()).getName());
        List<Comment> comments = commentMapper.selectByEntryId(entry_id);
        entryResponseType.setContent(entry.getContent());
        if (user_id == -1) {
            entryResponseType.setStars(false);
        } else {
            entryResponseType.setStars(starsMapper.selectByUserIdAndEntryId(user_id, entry_id) != null);
        }
        entryResponseType.setCommentList(comments);
        return entryResponseType;
    }

    @Override
    public void update(Entry entry) {
        // entryMapper.updateById(entry);
    }

    @Override
    public List<EntryResponseType> searchByCol(String col, String value, String order, Integer pageId) {
        Page<Entry> page = new Page<>(pageId, 40);
        QueryWrapper<Entry> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc(order)
                .like(col, value);
        entryMapper.selectPage(page, wrapper);
        List<Entry> entries = page.getRecords();
        return getEntryResponseTypes(entries);
    }

    @Override
    public List<EntryResponseType> searchByParams(Integer business_id, Integer education_id, Integer pageId, String order) {
        Page<Entry> page = new Page<>(pageId, 40);
        QueryWrapper<Entry> wrapper = new QueryWrapper<>();
        if (business_id != -1) {
            wrapper.eq("business_id", business_id);
        }
        if (education_id != -1) {
            wrapper.eq("education_id", education_id);
        }
        wrapper.orderByDesc(order);
        entryMapper.selectPage(page, wrapper);
        return getEntryResponseTypes(page.getRecords());
    }

    @Override
    public List<EntryResponseType> searchByUserId(int userId, String order) {
        QueryWrapper<Entry> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.orderByDesc(order);
        List<Entry> entries = entryMapper.selectList(wrapper);
        Collections.reverse(entries);
        return getEntryResponseTypes(entries);
    }


    @Override
    public int addStars(Integer user_id, Integer entry_id) {
        return entryMapper.insertStars(user_id, entry_id);
    }

    @Override
    public List<EntryResponseType> selectByUserId(int user_id, String order) {
        ArrayList<EntryResponseType> list = getEntryResponseTypes(entryMapper.selectByUserId(user_id, order));
        Collections.reverse(list);
        return list;
    }

    @Override
    public int cancelStars(Integer user_id, Integer entry_id) {
        return entryMapper.deleteStars(user_id, entry_id);
    }


    private ArrayList<EntryResponseType> getEntryResponseTypes(List<Entry> entries) {
        ArrayList<EntryResponseType> entryResponseTypes = new ArrayList<>();
        for (Entry entry : entries) {
            EntryResponseType entryResponseType = new EntryResponseType();
            entryResponseType.setId(entry.getId());
            entryResponseType.setCompany(entry.getCompany());
            entryResponseType.setCity(entry.getCity());
            entryResponseType.setSalary(entry.getSalary());
            entryResponseType.setPosition(entry.getPosition());
            entryResponseType.setEducation(educationMapper.selectById(entry.getEducationId()).getName());
            entryResponseType.setPublish_time(new SimpleDateFormat("yyyy-MM").format(entry.getPublishTime()));
            entryResponseType.setViews(entry.getViews());
            entryResponseTypes.add(entryResponseType);
        }
        return entryResponseTypes;
    }

}
